<?php
namespace app\index\Validate;

use think\facade\Cache;
use think\captcha\facade\Captcha;
use think\Validate;

class LoginValidate extends Validate
{
    protected $rule = [
        'email' => 'require|email|unique:member',
        "verify_code" => 'require|checkVerify',
        "code" => 'require|checkCode',
        'passwd'=>'require|length:6,18|alphaDash',
        'repassword'=>'require|confirm:passwd',
    ];

    protected $message = [
        'email.require' => '邮箱必须填写',
        'email.email' => '邮箱格式错误',
        'email.unique' => '邮箱存在',
        "code.require" => "邮箱验证码必填",
        "verify_code.require" => "图形验证码必填",
        "passwd.require" => "密码必填",
        "passwd.length" => "密码长度6-18位",
        "passwd.alphaDash"=>'密码必须是数字字母下划线组成',
        "repassword.require" => "确认密码必填",
        "repassword.confirm" => "两次密码不一致",
    ];

    protected $scene = [
        'send_email' => ['email',"verify_code"],
        'register' => ["email","passwd","repassword","code"],
        'forget' => ["email","passwd","repassword","code"],
    ];
    // 校验图形验证码
    protected function checkVerify($value)
    {
        if(!Captcha::check($value)) {
            return  '图形验证码错误';
        }
        return true;

    }

    protected function checkCode($value,$rule,$data)
    {
        return true;
          $key = config("constant.email_register_key").$data["email"];
          $code = Cache::store('redis')->get($key);
          if ($code!=$value) {
              return "验证码错误";
          }
          return true;
    }
}